﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Narwhunderful.Aphelia.Framework.Graphics;

namespace Narwhunderful.Aphelia.Framework.UI
{

    public abstract class UIContainerComponent<T> : UIComponent where T : UIComponent
    {

        UIComponentCollection<T> _components = null;
        public UIComponentCollection<T> Components
        {
            get
            {
                return _components;
            }
            set
            {
                _components = value;
            }
        }

        public UIContainerComponent(HUD hud)
            : base(hud)
        {
            _components = new UIComponentCollection<T>(hud, this, base.ParentScreen, base.SpriteBatch);
        }

        #region IUpdatableComponent

        protected override void InternalUpdate(GameTime gameTime)
        {
            base.InternalUpdate(gameTime);
            if (_components.UpdatableComponents.Count > 0) _components.Update(gameTime);
        }

        #endregion

        #region IDrawableComponent

        protected override void InternalDrawMiddleground(GameTime gameTime)
        {
            base.InternalDrawMiddleground(gameTime);
            if (_components.DrawableComponents.Count > 0) _components.Draw(gameTime);
        }

        #endregion

        #region ISpriteComponent

        public override SpriteBatch SpriteBatch
        {
            get
            {
                return base.SpriteBatch;
            }
            set
            {
                base.SpriteBatch = value;
                _components.SpriteBatch = value;
            }
        }

        #endregion

        #region IDesignableComponent

        protected override void OnDesigningChanged()
        {
            if (base.IsDesigning)
            {
                for (int x = 0; x < _components.UpdatableComponents.Count; x++)
                {
                    UIComponent component = _components.UpdatableComponents[x];
                    if (!component.IsDesigning) component.BeginDesigning();
                }
            }
            else
            {
                _components.Sort();
                for (int x = 0; x < _components.UpdatableComponents.Count; x++)
                {
                    UIComponent component = _components.UpdatableComponents[x];
                    if (component.IsDesigning) component.EndDesigning();
                }
            }
            base.OnDesigningChanged();
        }

        #endregion

        #region INavigableComponent

        protected override void DoNavigation(NavigationDirection direction)
        {
            base.DoNavigation(direction);
           
            if (base.NavigationState.PreviousComponent == null) return;
            if (base.NavigationState.PreviousComponent.ParentScreen != base.ParentScreen)
            {
                base.ParentScreen.Focus();
                return;
            }

            if (base.NavigationState.PreviousComponent.Parent == this)
            {  
                //int horizontalIndex = _components.HorizontalNavigableComponents.IndexOf(base.NavigationState.PreviousComponent);
                //int verticalIndex = _components.VerticalNavigableComponents.IndexOf(base.NavigationState.PreviousComponent);

                //bool foundComponent = false;
                //while (!foundComponent)
                //{
                //    if (direction == NavigationDirection.Up)
                //    {
                //        if (verticalIndex <= 0)
                //        {
                //            verticalIndex = _components.VerticalNavigableComponents.Count - 1;
                //        }
                //        else
                //        {
                //            verticalIndex--;
                //        }
                //        component = _focusedScreen.Components.VerticalNavigableComponents[verticalIndex];
                //        if (component.HorizontalNavagationIndex != previousComponent.HorizontalNavagationIndex) continue;
                //    }
                //    if (direction == NavigationDirection.Down)
                //    {
                //        if (verticalIndex >= _focusedScreen.Components.VerticalNavigableComponents.Count - 1)
                //        {
                //            verticalIndex = 0;
                //        }
                //        else
                //        {
                //            verticalIndex++;
                //        }
                //        component = _focusedScreen.Components.VerticalNavigableComponents[verticalIndex];
                //        if (component.HorizontalNavagationIndex != previousComponent.HorizontalNavagationIndex) continue;
                //    }
                //    if (direction == NavigationDirection.Left)
                //    {
                //        if (horizontalIndex <= 0)
                //        {
                //            horizontalIndex = _focusedScreen.Components.HorizontalNavigableComponents.Count - 1;
                //        }
                //        else
                //        {
                //            horizontalIndex--;
                //        }
                //        component = _focusedScreen.Components.HorizontalNavigableComponents[horizontalIndex];
                //        if (component.VerticalNavagationIndex != previousComponent.VerticalNavagationIndex) continue;
                //    }
                //    if (direction == NavigationDirection.Right)
                //    {
                //        if (horizontalIndex >= _focusedScreen.Components.HorizontalNavigableComponents.Count - 1)
                //        {
                //            horizontalIndex = 0;
                //        }
                //        else
                //        {
                //            horizontalIndex++;
                //        }
                //        component = _focusedScreen.Components.HorizontalNavigableComponents[horizontalIndex];
                //        if (component.VerticalNavagationIndex != previousComponent.VerticalNavagationIndex) continue;
                //    }
                //    if (!component.CanFocus) continue;
                //    foundComponent = true;
                //}
            }
        }

        #endregion

        #region IUIComponent

        protected internal override void OnRemoving(object sender, EventArgs e)
        {
            for (int x = 0; x < _components.UpdatableComponents.Count; x++)
            {
                string name = _components.UpdatableComponents[x].Name;
                if (base.HUD.ComponentNames.ContainsKey(name)) base.HUD.ComponentNames.Remove(name);
            }
            base.OnRemoving(sender, e);
        }

        #endregion

        #region IDisposeable

        protected override void Dispose(bool disposing)
        {
            if (disposing) _components.Dispose();
            base.Dispose(disposing);
        }

        #endregion

    }

}
